package com.bai.Service.Impl;

import com.bai.Constant.ResultConst;
import com.bai.Mapper.DepartmentMapper;
import com.bai.Pojo.Department;
import com.bai.Pojo.Prisoner;
import com.bai.Service.DepartmentService;
import com.bai.Utils.DtoResultUtil;
import com.bai.dto.DepChart;
import com.bai.dto.PageLimitDto;
import com.bai.dto.PublicResponseBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Transactional
public class DepartmentServiceImpl implements DepartmentService {

    public void setDepartmentMapper(DepartmentMapper departmentMapper) {
        this.departmentMapper = departmentMapper;
    }

    @Autowired
    public DepartmentMapper departmentMapper;

    //删除
    @Override
    public PublicResponseBody deleteDepart(Integer id) {
        int deleteDepart = departmentMapper.deleteDepart(id);
        if (deleteDepart>0){
            return DtoResultUtil.resultSuccess();
        }else {
            return DtoResultUtil.result(-1,ResultConst.delete_error);
        }
    }

    //添加
    @Override
    public PublicResponseBody addDepart(Department department) {
        int addDepartment = departmentMapper.addDepart(department);
        if (addDepartment>0){
            return DtoResultUtil.resultSuccess();
        }else {
            return DtoResultUtil.result(-1, ResultConst.add_error);
        }
    }

    @Override
    public PublicResponseBody editDepart(Department department) {
        int updateDepart = departmentMapper.updateDepart(department);
        if (updateDepart>0){
            return DtoResultUtil.resultSuccess();
        }else {
            return DtoResultUtil.result(-1,ResultConst.edit_error);
        }
    }

    @Override
    public PublicResponseBody getDepart(Integer id) {
        Department getdepartment = departmentMapper.getdepartment(id);
        if (getdepartment!=null){
            return DtoResultUtil.resultSuccess();
        }else {
            return DtoResultUtil.result(-1,ResultConst.query_error);
        }
    }

    @Override
    public PageLimitDto queryAllDepart(String departmentname,Integer offset, Integer limit) {

        PageLimitDto pageLimitDto=new PageLimitDto();
        pageLimitDto.setTotal(departmentMapper.queryAllPriCount(departmentname));
        pageLimitDto.setRows(departmentMapper.QueryAll(departmentname,offset,limit));
        return pageLimitDto;
    }

    @Override
    public PublicResponseBody queryAllDepartChart() {
        PageLimitDto pageLimitDto=new PageLimitDto();
        List<Department> departments = departmentMapper.QueryAll(null, 0, 20);
        List<String> departName=new ArrayList<>();
        List<Integer> departCount=new ArrayList<>();
        for (Department department : departments) {
            departCount.add(department.getCount());
            departName.add(department.getDepartmentname());
        }
        DepChart depChart=new DepChart();
        depChart.setDepartCount(departCount);
        depChart.setDepartName(departName);
        return DtoResultUtil.resultSuccess(depChart);
    }

    @Override
    public PublicResponseBody queryDepartDetailsById(Integer id) {
        Department queryDepartDetailsById = departmentMapper.queryDepartDetailsById(id);
        if (queryDepartDetailsById!=null){
            return DtoResultUtil.resultSuccess(queryDepartDetailsById);
        }else {
            return DtoResultUtil.result(-1,ResultConst.query_error);
        }
    }


}
